package com.example.callcenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.callcenter.entity.CallRecord;
import com.example.callcenter.entity.CallTask;
import com.example.callcenter.entity.Customer;
import com.example.callcenter.dto.PageQueryDTO;
import com.example.callcenter.dto.StatisticsDTO;
import com.example.callcenter.mapper.CallRecordMapper;
import com.example.callcenter.mapper.CallTaskMapper;
import com.example.callcenter.mapper.CustomerMapper;
import com.example.callcenter.service.IStatisticsService;
import com.example.callcenter.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class StatisticsServiceImpl implements IStatisticsService {

    @Autowired
    private CallTaskMapper taskMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private CallRecordMapper recordMapper;

    @Override
    public StatisticsDTO getSystemStatistics() {
        StatisticsDTO dto = new StatisticsDTO();

        // 任务统计
        dto.setTotalTasks(Math.toIntExact(taskMapper.selectCount(null)));
        dto.setCompletedTasks(Math.toIntExact(taskMapper.selectCount(
                new QueryWrapper<CallTask>().eq("status", 2))));

        // 客户统计
        dto.setTotalCustomers(Math.toIntExact(customerMapper.selectCount(null)));
        dto.setCalledCustomers(Math.toIntExact(customerMapper.selectCount(
                new QueryWrapper<Customer>().eq("status", 1))));

        // 外呼成功率
        long totalCalls = recordMapper.selectCount(null);
        long successCalls = recordMapper.selectCount(
                new QueryWrapper<CallRecord>().eq("result", "SUCCESS"));
        dto.setSuccessRate(totalCalls == 0 ? 0 : (double) successCalls / totalCalls);

        return dto;
    }

    @Override
    public ResultUtil getTaskStatistics(Long taskId) {
        // 查询任务基本信息
        CallTask task = taskMapper.selectById(taskId);
        if (task == null) {
            return ResultUtil.error("任务不存在");
        }

        // 查询任务相关统计
        Map<String, Object> stats = new HashMap<>();
        stats.put("taskInfo", task);
        stats.put("totalCustomers", customerMapper.selectCount(
                new QueryWrapper<Customer>().eq("task_id", taskId)));
        stats.put("callRecords", recordMapper.selectCount(
                new QueryWrapper<CallRecord>().eq("task_id", taskId)));

        return ResultUtil.success(stats);
    }

    @Override
    public ResultUtil getSalesStatistics(PageQueryDTO queryDTO) {
        IPage<Map<String, Object>> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        IPage<Map<String, Object>> result = recordMapper.selectSalesStatistics(page, queryDTO);
        return ResultUtil.success(result);
    }
}